if (!app.user) {app.redirect('/admin/login.php')}
jQuery(function() {

	function getAllSettings() {
		var data = _get('/app/index.php?d=admin&c=settings&m=get_all_settings');
		return data.settings;
	}


	function updateSettings() {
		var settings = getAllSettings();
		$('#inputCampaignStartTime').val(settings['campaign_start_time']);
		$('#inputCampaignTerms').val(settings['campaign_terms']);
	}


	function saveCampaignStartTime() {
		var eleCST = $('#inputCampaignStartTime');
		var time = eleCST.val();
		var result = _post('/app/index.php?d=admin&c=settings&m=set_setting', 
			{setting_key: 'campaign_start_time', setting_val: time});
		if (typeof result['error'] != 'undefined') {
			return showFailed(eleCST);
		}
		return showSuccess(eleCST);
	}

	function saveCampaignTerms() {
		var eleCT = $('#inputCampaignTerms');
		var terms = eleCT.val();
		var result = _post('/app/index.php?d=admin&c=settings&m=set_setting',
			{setting_key: 'campaign_terms', setting_val: terms});
		if (typeof result['error'] != 'undefined') {
			return showFailed(eleCT);
		}
		return showSuccess(eleCT);
	}

	function saveSettings() {
		saveCampaignStartTime();
		saveCampaignTerms();
	}

	$('form[name="settings"] [type="submit"]').click(function(e){
		e.preventDefault();
		saveSettings();
	})

	updateSettings();
})